package cn.lixu.pet.service.impl;

import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.pet.domain.PetType;
import cn.lixu.pet.mapper.PetTypeMapper;
import cn.lixu.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

   @Autowired
    private PetTypeMapper petTypeMapper;


//    @Override
//    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
//    public void save(Department department) {
//        //返回自增主键id
//        departmentMapper.save(department);
//        if (department.getParent_id() == null) {
//            Long id = department.getId();
//            department.setDirPath("/" + id);
//        } else {
//            //通过parent_id，获取上级的dirpath
//            Long parent_id = department.getParent_id();
//            //父级部门对象
//            Department parentDapartment = departmentMapper.loadById(parent_id);
//            //dirpath
//            department.setDirPath(parentDapartment.getDirPath() + "/" + department.getId());
//
//        }
//        //调用update方法，更新数据库
//        update(department);
//    }
//
//
//    @Override
//    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
//    public void update(Department department) {
//        if (department.getParent_id() == null) {
//            Long id = department.getId();
//            department.setDirPath("/" + id);
//        } else {
//            //通过parent_id，获取上级的dirpath
//            Long parent_id = department.getParent_id();
//            //父级部门对象
//            Department parentDapartment = departmentMapper.loadById(parent_id);
//            //dirpath
//            department.setDirPath(parentDapartment.getDirPath() + "/" + department.getId());
//
//        }
//        departmentMapper.update(department);
//
//    }


    //宠物树
    @Override
    public List<PetType> typeTree() {
        //获取全部部门信息
        List<PetType> list = petTypeMapper.loadAll();
        //准备一个map集合，用来装所有的部门
        HashMap<Long, PetType> map = new HashMap<>();
        //遍历所有的部门信息
        for (PetType petType : list) {
            //将部门信息装入map集合中
            map.put(petType.getId(), petType);
        }
        List<PetType> typeTree = new ArrayList<>();
        for (PetType petType : list) {
            if (petType.getPid() == null) {//顶级部门
                typeTree.add(petType);
            } else {
                //获取不是顶级部门的id
                Long pid = petType.getPid();
                //通过id在map里面去获取响应的部门信息
                PetType parentPetType = map.get(pid);
                //将自己添加到上级部门里面
                parentPetType.getChildren().add(petType);
            }
        }
        return typeTree;
    }


}

